﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Galaktika.BI.Silverlight.Services;

namespace Galaktika.BI.Silverlight.ClientApp.ViewModel
{
    public class QueryExecutionData : ObservableObjectCollection
    {
        public QueryExecutionData()
        {
        }

        public QueryData this[string id]
        {
            get
            {
                return (QueryData)base.Items.FirstOrDefault(o => ((QueryData)o).Id == id);
            }
        }

        protected override void InsertItem(int index, object item)
        {
            base.InsertItem(index, item);

            if (base.Count > 50)
            {
                base.RemoveAt(base.Count - 1);
            }
        }
    }

    public class QueryData : INotifyPropertyChanged
    {
        public QueryData()
        {
        }

        public string Id { get; set; }

        string m_QueryName;
        public string QueryName
        {
            get
            {
                return m_QueryName;
            }
            set
            {
                if (m_QueryName != value)
                {
                    m_QueryName = value;
                    this.OnPropertyChanged("QueryName");
                }
            }
        }

        QueryExecutionState m_QueryState;
        public QueryExecutionState QueryState
        {
            get
            {
                return m_QueryState;
            }
            set
            {
                if (m_QueryState != value)
                {
                    m_QueryState = value;
                    this.OnPropertyChanged("QueryState");
                    this.OnPropertyChanged("IsCancelEnabled");
                }
            }
        }

        string m_ExecutionTime;
        public string ExecutionTime
        {
            get
            {
                return m_ExecutionTime;
            }
            set
            {
                if (m_ExecutionTime != value)
                {
                    m_ExecutionTime = value;
                    this.OnPropertyChanged("ExecutionTime");
                }
            }
        }

        string m_Description;
        public string Description
        {
            get
            {
                return m_Description;
            }
            set
            {
                if (m_Description != value)
                {
                    m_Description = value;
                    this.OnPropertyChanged("Description");
                }
            }
        }

        string m_ErrorText;
        public string ErrorText
        {
            get
            {
                return m_ErrorText;
            }
            set
            {
                if (m_ErrorText != value)
                {
                    m_ErrorText = value;
                    this.OnPropertyChanged("ErrorText");
                }
            }
        }

        private string m_CommandId;
        public string CommandId
        {
            get
            {
                return m_CommandId;
            }
            set
            {
                if (m_CommandId != value)
                {
                    m_CommandId = value;
                    this.OnPropertyChanged("CommandId");
                }
            }
        }

        private string m_CommandIdDescriptor;
        public string CommandIdDescriptor
        {
            get
            {
                return m_CommandIdDescriptor;
            }
            set
            {
                if (m_CommandIdDescriptor != value)
                {
                    m_CommandIdDescriptor = value;
                    this.OnPropertyChanged("CommandIdDescriptor");
                }
            }
        }

        public bool IsCancelEnabled
        {
            get
            {
                return 
                    this.QueryState == QueryExecutionState.Executing ||
                    this.QueryState == QueryExecutionState.Queued;
            }
        }

        private string m_ResponseSize;
        public string ResponseSize
        {
            get
            {
                return m_ResponseSize;
            }
            set
            {
                if (m_ResponseSize != value)
                {
                    m_ResponseSize = value;
                    this.OnPropertyChanged("ResponseSize");
                }
            }
        }

        internal DateTime Started { get; set; }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
